#include "InputManager.h"
#include "../SGD wrappers/CSGD_Direct3D.h"
#include "../SGD wrappers/CSGD_DirectInput.h"

InputManager* InputManager::GetInstance(void)
{
	static InputManager instance;
	return &instance;
}

void InputManager::Init(HWND hWnd, HINSTANCE hInstance)
{
	DInput = CSGD_DirectInput::GetInstance();
	DInput->InitDirectInput(hWnd, hInstance, DI_MOUSE | DI_KEYBOARD | DI_JOYSTICKS, 0);

	POINT cursor;
	GetCursorPos(&cursor);

	prevCursorPosX = currCursorPosX = (float)cursor.x;
	prevCursorPosX = prevCursorPosY = (float)cursor.y;

	inputType = MOUSEKEYBOARD;

#ifdef ARCADEMACHINE
	inputType = ARCADE;
#else
	inputType = MOUSEKEYBOARD;
#endif
	this->hinstance = hInstance;
	this->hWnd = hWnd;
}

void InputManager::Update(float fElapsedTime)
{
	DInput->ReadDevices();

	if(fElapsedTime != 0.0f)
	{
		currCursorPosX = (float)CursorPosition().x;
		currCursorPosY = (float)CursorPosition().y;

		currCursorVelX = ((currCursorPosX - prevCursorPosX) / fElapsedTime);
		currCursorVelY = ((currCursorPosY - prevCursorPosY) / fElapsedTime);

		prevCursorPosX = currCursorPosX;
		prevCursorPosY = currCursorPosY;
	}
}

POINT InputManager::GetCurrentVelocity()
{
	POINT temp;
	temp.x = (long)currCursorVelX;
	temp.y = (long)currCursorVelY;

	return temp;
}

void InputManager::ChangeInputType(EInputType type)
{
#ifndef ARCADEMACHINE
	inputType = type;
#endif
}

void InputManager::Shutdown(void)
{
	DInput->ShutdownDirectInput();
	DInput = NULL;
}

bool InputManager::MoveUp()
{
	switch(inputType)
	{
	case MOUSEKEYBOARD:
		{
			if(DInput->KeyPressed( DIK_UP ))
				return true;
		}
		break;
	case ARCADE:
		{
			if(DInput->JoystickGetLStickDirPressed(DIR_UP))
				return true;
		}
		break;
	}
	return false;
}

bool InputManager::MoveDown()
{
	switch(inputType)
	{
	case MOUSEKEYBOARD:
		{
			if(DInput->KeyPressed( DIK_DOWN ))
				return true;
		}
		break;
	case ARCADE:
		{
			if(DInput->JoystickGetLStickDirPressed(DIR_DOWN))
				return true;
		}
		break;
	}
	return false;
}

bool InputManager::MoveLeft()
{
	switch(inputType)
	{
	case MOUSEKEYBOARD:
		{
			if(DInput->KeyPressed( DIK_LEFT ))
				return true;
		}
		break;
	case ARCADE:
		{
			if(DInput->JoystickGetLStickDirPressed(DIR_LEFT))
				return true;
		}
		break;
	}
	return false;
}

bool InputManager::MoveRight()
{
	switch(inputType)
	{
	case MOUSEKEYBOARD:
		{
			if(DInput->KeyPressed( DIK_RIGHT ))
				return true;
		}
		break;
	case ARCADE:
		{
			if(DInput->JoystickGetLStickDirPressed(DIR_RIGHT))
				return true;
		}
		break;
	}
	return false;
}

bool InputManager::Action()
{
	switch(inputType)
	{
		case MOUSEKEYBOARD:
			{
				if(DInput->MouseButtonDown(0))
					return true;
			}
			break;
		case ARCADE:
			{
				if(DInput->MouseButtonDown(0))
					return true;
			}
			break;
	}
	return false;
}

bool InputManager::Upgrade()
{
	switch(inputType)
	{
		case MOUSEKEYBOARD:
			{
				if(DInput->MouseButtonDown(1))
					return true;
			}
			break;
		case ARCADE:
			{
				if(DInput->MouseButtonDown(1))
					return true;
			}
			break;
	}
	return false;
}

bool InputManager::EquiptPinch()
{
	switch(inputType)
	{
		case MOUSEKEYBOARD:
			{
				if(DInput->KeyPressed(DIK_1))
					return true;
			}
			break;
		case ARCADE:
			{
				if(DInput->JoystickButtonPressed(0))
					return true;
			}
			break;
	}
	return false;
}

bool InputManager::EquiptGauntlet()
{
	switch(inputType)
	{
		case MOUSEKEYBOARD:
			{
				if(DInput->KeyPressed(DIK_2))
					return true;
			}
			break;
		case ARCADE:
			{
				if(DInput->JoystickButtonPressed(3))
					return true;
			}
			break;
	}
	return false;
}

bool InputManager::EquiptPoison()
{
	switch(inputType)
	{
		case MOUSEKEYBOARD:
			{
				if(DInput->KeyPressed(DIK_2))
					return true;
			}
			break;
		case ARCADE:
			{
				if(DInput->JoystickButtonPressed(2))
					return true;
			}
			break;
	}	
	return false;
}

bool InputManager::EquiptBoulder()
{
	switch(inputType)
	{
		case MOUSEKEYBOARD:
			{
				if(DInput->KeyPressed(DIK_3))
					return true;
			}
			break;
		case ARCADE:
			{
				if(DInput->JoystickButtonPressed(1))
					return true;
			}
			break;
	}
	return false;
}

bool InputManager::Pause()
{
	switch(inputType)
	{
		case MOUSEKEYBOARD:
			{
				if(DInput->KeyPressedEx(DIK_P))
					return true;
			}
			break;
		case ARCADE:
			{
				if(DInput->JoystickButtonPressed(6))
					return true;
			}
			break;
	}
	return false;
}

bool InputManager::Escape()
{
	if(DInput->KeyPressedEx(DIK_ESCAPE))
		return true;

	if(DInput->JoystickButtonPressedEx(6))
		return true;

	return false;
}

bool InputManager::Enter()
{
	switch(inputType)
	{
		case MOUSEKEYBOARD:
			{
				if(DInput->KeyPressedEx(DIK_RETURN))
					return true;
			}
			break;
		case ARCADE:
			{
				if(DInput->MouseButtonDown(0))
					return true;
			}
			break;
	}	

	return false;
}

POINT InputManager::CursorPosition()
{
	POINT cursor;
	GetCursorPos(&cursor);
	ScreenToClient(hWnd, &cursor);
	return cursor;
}

void InputManager::SetCursorPosition(int x, int y)
{
	POINT cursor;
	cursor.x = x;
	cursor.y = y;
	ClientToScreen(hWnd, &cursor);
	SetCursorPos(cursor.x, cursor.y);
}
